package com.example.androidcompletesec.actionbar;

import com.example.androidcompletesec.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class ActionTab extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actiontab);
		ActionBar ab = getActionBar();
		ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		for (int i=0;i<3;i++) {
			ActionBar.Tab tab = ab.newTab();
			String Cap = "Tab" + (i + 1);
			tab.setText(Cap);
			TabFragment frag = TabFragment.newInstance(Cap);
			tab.setTabListener(new TabListener(frag));
			ab.addTab(tab);
		}
		if (savedInstanceState != null) {
			int seltab = savedInstanceState.getInt("seltab");
			ab.setSelectedNavigationItem(seltab);
		}
	}
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("seltab", getActionBar().getSelectedNavigationIndex());
	}
	private class TabListener implements ActionBar.TabListener {
		private Fragment mFragment;
		public TabListener(Fragment fragment) {
			mFragment = fragment;
		}
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.add(R.id.tabparent, mFragment, "tag");
		}
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			ft.remove(mFragment);
		}
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
		}
	}
	public static class TabFragment extends Fragment {
		public static TabFragment newInstance(String text) {
			TabFragment frag = new TabFragment();
			Bundle args = new Bundle();
			args.putString("text", text);
			frag.setArguments(args);
			return frag;
		}
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			String text = "";
			Bundle args = getArguments();
			if (args != null) {
				text = args.getString("text");
			}
			View linear = inflater.inflate(R.layout.actiontabfragment, container, false);
			TextView textview = (TextView)linear.findViewById(R.id.content);
			textview.setText(text);
			return linear;
		}
	}
}